<!--
 * @Descripttion: 搜索工具类-保养项目
 * @LastEditors: xzh
 * @LastEditTime: 2023-09-15 14:14:38
-->
<template>
  <el-skeleton :loading="searchUpkeepItemParam.loading" animated>
    <template #template>
      <el-skeleton-item class="el-col-11" />
      <el-skeleton-item class="el-col-11" />
    </template>
    <template #default>
      <el-col :span="11">
        <el-input
          v-model="searchData.keyWord"
          clearable
          size="large"
          :placeholder="$t('page.equipUpkeepArr.upkeepItemArr.searchCondition')"
        />
      </el-col>
      <el-col :span="11">
        <el-select
          v-model="searchData.maintenanceLevel"
          size="large"
          :placeholder="
            $t('common.select') + $t('page.equipManageArr.equipState')
          "
        >
          <el-option
            v-for="upkeepLevel in searchUpkeepItemParam.upkeepLevelArr"
            :key="upkeepLevel.value"
            :label="upkeepLevel.label"
            :value="upkeepLevel.value"
          />
        </el-select>
      </el-col>
    </template>
  </el-skeleton>
</template>

<script setup lang="ts">
import { ref, reactive, toRef, onMounted } from "vue";
import { object_Inf } from "@/utils/typings/index.type";
import Dile_APi from "@/api";
import Dile_Methods from "@/utils/methods";
const props = defineProps({
  //搜索数据
  searchData: {
    type: Object,
    requited: true,
    default: {},
  },
});

/**
 * @Author: xzh
 * @Descripttion: 搜索保养项目参数
 * @Param:
 */
let searchUpkeepItemParam: object_Inf = reactive({
  loading: true,
  //保养级别
  upkeepLevelArr: [],
});
/**
 * @Author: xzh
 * @Descripttion: 默认搜索数据
 * @Param:
 */
let defaultData = () => ({
  keyWord: "",
  maintenanceLevel: "", //保养级别
});
/**
 * @Author: xzh
 * @Descripttion: 初始化
 * @Param:
 */
onMounted(async () => {
  let loading = toRef(searchUpkeepItemParam, "loading");
  try {
    loading.value = true;
    reset();
    //加载搜索资源
    await initSearchResource();
  } finally {
    loading.value = false;
  }
});

/**
 * @Author: xzh
 * @Descripttion: 加载表单资源
 * @Param:
 */
let initSearchResource = async () => {
  //加载保养级别资源
  await initUpkeepLevelArrResource();
};

/**
 * @Author: xzh
 * @Descripttion: 加载保养级别资源
 * @Param:
 */
let initUpkeepLevelArrResource = async () => {
  let upkeepLevelArr = toRef(searchUpkeepItemParam, "upkeepLevelArr");
  //加载所有保养项目级别
  upkeepLevelArr.value = Dile_Methods.filterDataArr(
    (await Dile_APi.getUpkeepLevelList())?.map((upkeepLevel: object_Inf) => ({
      value: String(upkeepLevel.id),
      label: upkeepLevel.name,
    }))
  );
};

/**
 * @Author: xzh
 * @Descripttion: 重置
 * @Param:
 */
let reset = () => {
  Object.assign(props.searchData, defaultData());
};

defineExpose({
  reset,
});
</script>
<style lang="scss" scoped></style>
